﻿using System;
using System.IO;
using System.Net;
using System.Security.Principal;

namespace WeatherService.Helpers
{
    public static class Logger
    {
        private static int monthstoLog = 2;
        public static void ErrorLog(string logString)
        {
            //Get the current directory
            string folder = AppDomain.CurrentDomain.BaseDirectory;

            if (File.Exists(folder + "\\WeatherServiceErrorLog - " + DateTime.Now.AddMonths(-monthstoLog).Month.ToString() + ".txt"))
            {
                try
                {
                    File.Delete(folder + "\\WeatherServiceErrorLog - " + DateTime.Now.AddMonths(-monthstoLog).Month.ToString() + ".txt");
                }
                catch { }
            }
            using (FileStream stream = new FileStream(folder + "\\WeatherServiceErrorLog - " + DateTime.Now.Month.ToString() + ".txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.BaseStream.Seek(0L, SeekOrigin.End);
                    string timestamp = DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm");
                    writer.WriteLine("Error has occurred : " + timestamp + " - " + logString);


                }
            }

        }

        public static void StatusLog(string logString)
        {
            //Get the current directory
            string folder = AppDomain.CurrentDomain.BaseDirectory;

            if (File.Exists(folder + "\\WeatherServiceStatusLog - " + DateTime.Now.AddMonths(-monthstoLog).Month.ToString() + ".txt"))
            {
                try
                {
                    File.Delete(folder + "\\WeatherServiceStatusLog - " + DateTime.Now.AddMonths(-monthstoLog).Month.ToString() + ".txt");
                }
                catch { }
            }
            using (FileStream stream = new FileStream(folder + "\\WeatherServiceStatusLog - " + DateTime.Now.Month.ToString() + ".txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.BaseStream.Seek(0L, SeekOrigin.End);
                    string timestamp = DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm");
                    writer.WriteLine("Status : " + timestamp + " - " + logString);
                }
            }

        }

        private static void emailErrors(string message)
        {
            if (!message.Contains("Email"))
            {

                //Get local computerName
                string serverName = Dns.GetHostName();

                //Get logged in user
                string serverUserName = WindowsIdentity.GetCurrent().Name;
                //Get public IP of Server
                IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName());
                string serverpublicIP = IPHelper.GetExternalIp().ToString();//IPHost.AddressList[0].ToString();
                message += System.Environment.NewLine + "<br>*****************************************************<br>" + System.Environment.NewLine +
                           "This email originated from " + serverName + " -- from User: " + serverUserName + " and the Public IP address of the site is " + serverpublicIP +
                           System.Environment.NewLine + "<br>*****************************************************";
                
            }
        }
    }
}
